<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="'审批人员设置(' + activityName + ')'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px">
      <el-form-item label="审批人类型" prop="approveType">
        <el-select v-model="form.approveType" placeholder="请选择审批人类型">
          <el-option
            v-for="(item) in dicts.flow_approve_type"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审批人" prop="approveValue">
        <el-input v-if="form.approveType === 'USER'" :readonly="true" v-model="form.approveName" placeholder="请选择用户">
          <el-button slot="append" icon="el-icon-search" @click="selectUser" />
        </el-input>
        <el-input v-else-if="form.approveType === 'ROLE'" :readonly="true" v-model="form.approveName" placeholder="请选择角色">
          <el-button slot="append" icon="el-icon-search" @click="selectRole" />
        </el-input>
        <el-input v-else v-model="form.approveValue" :readonly="true" :disabled="true" value="-1"/>
      </el-form-item>
      <el-form-item v-if="isMultiinstance" label="会签投票方式" prop="voteMethod">
        <el-radio v-for="item in dicts.flow_vote_method" :key="item.id" v-model="form.voteMethod" :label="item.dictCode">{{ item.dictName }}</el-radio>
      </el-form-item>
      <el-form-item v-if="isMultiinstance" label="投票数" prop="voteValue">
        <el-input-number v-model="form.voteValue" :precision="0" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
    <userSelectList ref="userSelectList" :multi_select="true" @userSelectCallback="userSelectCallback" />
    <roleSelectList ref="roleSelectList" :multi_select="true" @roleSelectCallback="roleSelectCallback" />
  </el-dialog>
</template>

<script>
import { doSaveConfigUser, getConfigDetailInfo } from '@/api/system/workflowConfig'
import userSelectList from '@/views/system/user/module/userSelectList'
import roleSelectList from '@/views/system/role/module/roleSelectList'

export default {
  components: { userSelectList, roleSelectList },
  dictTypes: ['flow_approve_type', 'flow_vote_method'],
  props: {
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      isMultiinstance: false,
      activityName: '',
      // approveName: '',
      nodeInfo: {},
      configMngData: {},
      form: {
        approveType: 'USER',
        approveValue: '-1',
        approveName: '',
        voteType: 'PASS',
        voteMethod: 'NUM', // 百分比/绝对票数
        voteValue: ''
      },
      rules: {
        'approveType': [
          { required: true, message: '请选择审批类型', trigger: 'blur' }
        ],
        'voteValue': [
          { required: true, message: '请输入投票数', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'form.approveType'(value) {
      if (value === 'MGR') {
        this.form.approveValue = '-1'
      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initSet(mainId, activityId) {
      getConfigDetailInfo(mainId, activityId).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    selectUser() {
      const _this = this.$refs.userSelectList
      _this.loadData()
      _this.dialog = true
    },
    userSelectCallback(selectedRows, userSelectObj) {
      let approveName = ''
      let approveValue = ''
      selectedRows.forEach(row => {
        approveValue = row.userAccount + ',' + approveValue
        approveName = row.userName + ',' + approveName
      })
      this.form.approveName = approveName
      // this.approveName = approveName
      this.form.approveValue = approveValue

      userSelectObj.cancel()
      userSelectObj.loading = false
    },
    selectRole() {
      const _this = this.$refs.roleSelectList
      _this.init()
      _this.dialog = true
    },
    roleSelectCallback(selectedRows, roleSelectObj) {
      let approveName = ''
      let approveValue = ''
      selectedRows.forEach(row => {
        approveValue = row.id + ',' + approveValue
        approveName = row.roleName + ',' + approveName
      })
      this.form.approveName = approveName
      this.form.approveValue = approveValue

      roleSelectObj.cancel()
      roleSelectObj.loading = false
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.doSaveConfigUser()
        }
      })
    },
    doSaveConfigUser() {
      this.form.mainId = this.configMngData.id
      this.form.activityId = this.nodeInfo.activityId
      this.form.activityName = this.activityName
      if (!this.isMultiinstance) {
        this.form.voteType = ''
        this.form.voteValue = ''
        this.form.voteMethod = ''
      }
      doSaveConfigUser(this.form).then(res => {
        this.resetForm()
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
